package com.boe.rnmf.pkg.controller;

import com.boe.rnmf.entity.UserEntity;
import com.boe.rnmf.common.enums.UserStatusEnum;
import com.boe.rnmf.vo.UserVO;
import com.boe.rnmf.vo.query.UserQueryParam;
import com.boe.rnmf.vo.response.BaseResponse;
import com.boe.rnmf.pkg.service.UserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 用户列表
     *
     * @param userQueryParam 查询参数
     * @return
     */
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse queryUserList(UserQueryParam userQueryParam) {
        try {
            userQueryParam.setOffset((userQueryParam.getPageIndex() - 1) * userQueryParam.getPageIndex());
            List<UserEntity> userList = userService.queryUserList(userQueryParam);
            return BaseResponse.returnData(userList);
        } catch (Exception e) {
            logger.error("查询用户列表失败", e);
            return BaseResponse.error();
        }
    }

    /**
     * 冻结用户
     *
     * @param userId 用户Id
     * @return
     */
    @RequestMapping(value = "/user/freeze/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public BaseResponse freezeUser(@PathVariable(name = "id") String userId) {
        try {
            int count = userService.freezeUser(userId, UserStatusEnum.FREEZED.getCode());
            if (count > 0) {
                return BaseResponse.returnOk("冻结用户成功");
            } else {
                return BaseResponse.error();
            }

        } catch (Exception e) {
            logger.error("冻结用户失败", e);
            return BaseResponse.error();
        }
    }

    /**
     * 修改用户
     *
     * @param userId 用户Id
     * @param user   修改信息
     * @return
     */
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public BaseResponse modifyUser(@PathVariable(name = "id") String userId, UserEntity user) {
        try {
            UserEntity userEntity = userService.get(userId);
            userEntity.setName(user.getName());
            userEntity.setEmail(user.getEmail());
            userEntity.setMobile(user.getMobile());
            int count = userService.update(userEntity);
            if (count > 0) {
                return BaseResponse.returnOk();
            } else {
                return BaseResponse.error();
            }

        } catch (Exception e) {
            logger.error("修改用户失败", e);
            return BaseResponse.error();
        }
    }

    /**
     * 删除用户
     *
     * @param userId 用户Id
     * @return
     */
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public BaseResponse deleteUser(@PathVariable(name = "id") String userId) {
        try {
            int count = userService.deleteUserById(userId);
            if (count > 0) {
                return BaseResponse.returnOk("删除成功");
            } else {
                return BaseResponse.error();
            }

        } catch (Exception e) {
            logger.error("删除用户失败", e);
            return BaseResponse.error();
        }
    }


    /**
     * 创建用户
     *
     * @param userVO 用户信息
     * @return
     */
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addUser(@RequestBody UserVO userVO) {
        try {
            Assert.notNull(userVO.getAccount(), "account must not null");
            Assert.notNull(userVO.getName(), "name must not null");
            Assert.notNull(userVO.getEmail(), "email must not null");
            Assert.notNull(userVO.getPassword(), "password must not null");
            Assert.notNull(userVO.getRoleId(), "roleId must not null");
            Assert.notNull(userVO.getOrgId(), "orgId must not null");
            userService.saveUser(userVO);
            return BaseResponse.returnOk("创建成功");
        } catch (IllegalArgumentException e) {
            logger.error("创建用户失败", e);
            return BaseResponse.error(e.getMessage());
        } catch (Exception e) {
            logger.error("创建用户失败", e);
            return BaseResponse.error();
        }
    }


}
